package com.sjh.net.loading;

import android.app.Dialog;
import android.content.Context;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.sjh.baseutils.R;
import com.sjh.baseutils.loading.LoadingType;


/**
 * @author chenli
 * @create 2018/6/7
 * @Describe
 */
public class LoadingDialogUtil {
    /**
     * 初始化进度条
     */
    public static CustomProgressDialog initCPD(Context context) {
        CustomProgressDialog pd = CustomProgressDialog.createDialog(context);
        pd.setCanceledOnTouchOutside(true);
        pd.closeOutSideWindow(true);
        pd.setMessage(context.getString(R.string.dl_waiting));
        return pd;
    }

    /**
     * 显示对话框
     */
    public static void showDialog(@Nullable Dialog dialog) {
        try{
            if (dialog != null && !dialog.isShowing()) {
                dialog.show();
            }
        }catch (Exception e){
            dimssDialog(dialog);
        }

    }

    /**
     * 关闭对话框
     */
    public static void dimssDialog(@Nullable Dialog dialog) {
        try{
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }catch (Exception e){
            dialog = null;
        }
    }

    /**
     * 判断Dialog是否正在显示
     * @param dialog
     * @return
     */
    public static boolean isShowing(@Nullable Dialog dialog){
        try{
            if (dialog != null && dialog.isShowing()) {
                return true;
            }
        }catch (Exception ignored){

        }
        return false;
    }

    /**
     * 获取加载框
     * @param loadingType
     * @return
     */
    @Nullable
    public static Dialog getLoadingDialog(LoadingType loadingType){
        Dialog dialog = null;
        Context context = ActivityUtils.getTopActivity();
        if(context == null) {
            return null;
        }
        switch (loadingType){
            case GENERAL:
                CustomProgressDialog pd = CustomProgressDialog.createDialog(context);
                pd.setCanceledOnTouchOutside(true);
                pd.closeOutSideWindow(true);
                pd.setMessage(context.getString(R.string.dl_waiting));
                dialog = pd;
                break;
            case GENERAL_AND_NO_CANCEL:
                CustomProgressDialog pd2 = CustomProgressDialog.createDialog(context);
                pd2.setCancelable(false);
                pd2.setCanceledOnTouchOutside(false);
                pd2.closeOutSideWindow(false);
                pd2.setMessage(context.getString(R.string.dl_waiting));
                dialog = pd2;
                break;
            case NONE:break;
            default:break;
        }
        return dialog;
    }
}